package com.threeonemeeting.activity;

import java.util.ArrayList;
import java.util.Map;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.snda.lib.http.TaskManager;
import com.snda.lib.ui.MessageBox;
import com.threeonemeeting.Const;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.adapter.MessageDetailListAdapter;
import com.threeonemeeting.model.MessageDetail;
import com.threeonemeeting.model.MessageInfo;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.tab.MultiActivityTab;
import com.threeonemeeting.task.DeleteMessageTask;
import com.threeonemeeting.task.GetMessageDetailListTask;
import com.threeonemeeting.task.HttpConst;
import com.threeonemeeting.task.ReplyMessageTask;
import com.threeonemeeting.util.MiscUtil;

public class MessageDetailListActivity extends BaseTabActivity implements
		ICallBack {

	private int nCurrentPages = 0;
	private int nMessageId = 0;
	private int nMessageType = 0;

	private MessageDetailListAdapter mAdapter;
	private MessageInfo parentMessage;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_message_detail_list);
		MiscUtil.setBackBtnListener(this);

		parentMessage = (MessageInfo) getIntent().getExtras().getSerializable(
				"message");
		nMessageId = parentMessage.nMessageId;
		nMessageType = getIntent().getExtras().getInt("type");

		LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
		LinearLayout listLayout = (LinearLayout) findViewById(R.id.list_layout);
		loadingLayout.setVisibility(View.VISIBLE);
		listLayout.setVisibility(View.GONE);

		// ListView
		ListView listViewMessageDetail = (ListView) findViewById(R.id.lv_message_detail);
		listViewMessageDetail.setDividerHeight(0);

		mAdapter = new MessageDetailListAdapter(this);
		listViewMessageDetail.setAdapter(mAdapter);

		// From
		ImageView fromImageView = (ImageView) findViewById(R.id.from_image);
		String strImageUrl = Const.URL.BASE_HEADERIMG_URL
				+ parentMessage.nFromUserId;
		MiscUtil.setImageView(fromImageView, strImageUrl);

		TextView fromTextView = (TextView) findViewById(R.id.from_name);
		fromTextView.setText(parentMessage.strFromName);

		// to
		ImageView toImageView = (ImageView) findViewById(R.id.to_image);
		strImageUrl = Const.URL.BASE_HEADERIMG_URL + parentMessage.nToUserId;
		MiscUtil.setImageView(toImageView, strImageUrl);

		TextView toNameView = (TextView) findViewById(R.id.to_name);
		toNameView.setText(parentMessage.strToUserName);

		// 按钮事件
		// 注册按钮
		Button btnReply = (Button) findViewById(R.id.btn_reply);
		OnClickListener replyBtnClickListener = new OnClickListener() {

			public void onClick(View v) {

				EditText editReply = (EditText) findViewById(R.id.editReplyContent);
				if (editReply.getText().toString() == null
						&& editReply.getText().toString().equalsIgnoreCase("") == true) {
					MessageBox.showToast(MessageDetailListActivity.this,
							"请输入回复的内容");
					return;
				}

				String strHint = getResources().getString(
						R.string.string_reply_message);
				if (editReply.getText().toString().equalsIgnoreCase(strHint) == true) {
					MessageBox.showToast(MessageDetailListActivity.this,
							"请输入回复的内容");
					return;
				}

				ReplyMessageTask replyTask = new ReplyMessageTask(
						MessageDetailListActivity.this,
						MessageDetailListActivity.this);
				Bundle params = new Bundle();
				params.putString("tid", String.valueOf(nMessageId));
				params.putString(
						"uid",
						String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
				params.putString(
						"pass",
						DataCenter.getInstance().currentLoginUserInfo.strPassword);
				params.putString("content", editReply.getText().toString());

				TaskManager.addTask(replyTask, params, true);

			}
		};
		btnReply.setOnClickListener(replyBtnClickListener);

		//
		final EditText editReply = (EditText) findViewById(R.id.editReplyContent);
		editReply.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_reply_message);
				String strSearch = editReply.getText().toString();
				if (hasFocus == true) {
					if (strSearch.equalsIgnoreCase(strHint) == true) {
						editReply.setText("");
					}
				} else {

					if (strSearch.equalsIgnoreCase("") == true) {
						editReply.setText(strHint);
					}
				}
			}
		});

		// 删除
		Button btnDelete = (Button) findViewById(R.id.btn_delete);
		OnClickListener deleteBtnClickListener = new OnClickListener() {

			public void onClick(View v) {
				DialogInterface.OnClickListener comfirmListener = new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						DeleteMessageTask deleteTask = new DeleteMessageTask(
								MessageDetailListActivity.this,
								MessageDetailListActivity.this);
						Bundle params = new Bundle();
						params.putString("uid", String.valueOf(DataCenter
								.getInstance().currentLoginUserInfo.nUserId));
						params.putString(
								"pass",
								DataCenter.getInstance().currentLoginUserInfo.strPassword);
						params.putString("tid", String.valueOf(nMessageId));
						params.putString("type", String.valueOf(nMessageType));
						TaskManager.addTask(deleteTask, params, false);
					}
				};

				MultiActivityTab tabWnd = (MultiActivityTab) MessageDetailListActivity.this
						.getParent();
				MessageBox box = new MessageBox(tabWnd);
				box.showDialog("提示", "确定要删除该消息吗", comfirmListener);

			}
		};
		btnDelete.setOnClickListener(deleteBtnClickListener);

		GetMessageDetailListTask task = new GetMessageDetailListTask(this, this);
		Bundle params = new Bundle();
		params.putString("tid", String.valueOf(nMessageId));
		params.putString(
				"uid",
				String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
		params.putString("pass",
				DataCenter.getInstance().currentLoginUserInfo.strPassword);
		params.putString("page", String.valueOf(nCurrentPages));
		TaskManager.addTask(task, params, true);
	}

	public void doCallBack(Map<String, Object> mapData) {
		Integer msgType = (Integer) mapData.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			return;
		}

		Integer nError = (Integer) mapData.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {
			return;
		}

		if (msgType == HttpConst.TASKTYPE_GETMESSAGEDETAILLIST) {
			LinearLayout loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
			LinearLayout listLayout = (LinearLayout) findViewById(R.id.list_layout);
			loadingLayout.setVisibility(View.GONE);
			listLayout.setVisibility(View.VISIBLE);

			@SuppressWarnings("unchecked")
			ArrayList<MessageDetail> listMessage = (ArrayList<MessageDetail>) mapData
					.get("list");
			mAdapter.setDetailList(listMessage);

			// 根结点
			MessageDetail detail = new MessageDetail();
			detail.nFromId = parentMessage.nFromUserId;
			detail.strFromName = parentMessage.strFromName;
			detail.strDate = parentMessage.strFromTime;
			detail.strText = parentMessage.strFromMsg;
			detail.nLayoutID = R.layout.lvi_message_me_say_item;
			mAdapter.insert(detail);

			mAdapter.notifyDataSetChanged();
		} else if (msgType == HttpConst.TASKTYPE_REPLYMESSAGE) {
			// 回复消息

			Boolean bSuccess = (Boolean) mapData.get("result");
			if (bSuccess == null) {
				MessageBox.showToast(getApplicationContext(), "回复失败");
				return;
			}

			EditText editReply = (EditText) findViewById(R.id.editReplyContent);
			editReply.setText("");

			MessageBox.showToast(getApplicationContext(), "回复成功，滚动到底部查看");

			// 重新刷新列表
			GetMessageDetailListTask task = new GetMessageDetailListTask(this,
					this);
			task.setRefresh(true);
			Bundle params = new Bundle();
			params.putString("tid", String.valueOf(nMessageId));
			params.putString(
					"uid",
					String.valueOf(DataCenter.getInstance().currentLoginUserInfo.nUserId));
			params.putString("pass",
					DataCenter.getInstance().currentLoginUserInfo.strPassword);
			params.putString("page", String.valueOf(nCurrentPages));
			TaskManager.addTask(task, params, true);
		} else if (msgType == HttpConst.TASKTYPE_DELETEMESSAGE) {

			Boolean bSuccess = (Boolean) mapData.get("result");
			if (bSuccess == null) {
				MessageBox.showToast(getApplicationContext(), "删除消息失败");
				return;
			}

			MessageBox.showToast(getApplicationContext(), "删除成功");

			Bundle b = new Bundle();
			b.putBoolean("refresh", true);
			b.putInt("type", nMessageType);
			startActivity(null, "MainMessageActivity", b);
		}
	}
}
